#!/bin/bash

# 项目根目录
PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

# 清理构建目录
clean_build() {
    echo "Cleaning build directories..."
    rm -rf "$PROJECT_ROOT/proto_generator/build"
    rm -rf "$PROJECT_ROOT/code_builder/build"
    rm -rf "$PROJECT_ROOT/generated"
}

# 仅生成 proto 文件
generate_proto() {
    echo "Generating protobuf files..."
    mkdir -p "$PROJECT_ROOT/proto_generator/build"
    cd "$PROJECT_ROOT/proto_generator/build"
    cmake .. && cmake --build .
}

# 仅编译代码
build_code() {
    echo "Building application code..."
    mkdir -p "$PROJECT_ROOT/code_builder/build"
    cd "$PROJECT_ROOT/code_builder/build"
    cmake .. && cmake --build .
}

# 完整构建（生成 + 编译）
full_build() {
    generate_proto
    build_code
}

# 根据参数执行操作
case "$1" in
    clean)
        clean_build
        ;;
    generate)
        generate_proto
        ;;
    build)
        build_code
        ;;
    all|"")
        full_build
        ;;
    *)
        echo "Usage: $0 [clean|generate|build|all]"
        exit 1
        ;;
esac

echo "Operation completed successfully!"
